import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.odps.udf.UDF;

public class SpliderManKindGoods extends UDF {
	
	public String evaluate(String html) {
		try {
			//HttpClientAPI HttpClientAPI = new HttpClientAPI();
			//HttpResult doGet = HttpClientAPI
			//		.doGet("https://www.mankind.co.uk/nudestix-concealer-pencil/11065858.html");
			//Document document = Jsoup.parse(doGet.getBody());
			//String html = document.getElementsByClass("body-wrap").toString();
			
			JSONObject returnObj = new JSONObject();
			if (StringUtils.isNotBlank(html)) {
				Document doc = Jsoup.parse(html);
				
				//获取分类目录
				Elements cateClass = doc.getElementsByClass("breadcrumbs_container");
				if(null != cateClass && cateClass.size() == 1) {
					System.out.println(cateClass.get(0).text());
					Elements liTags = cateClass.get(0).getElementsByTag("li");
					StringBuffer sb = new StringBuffer();
					for(Element li :liTags) {
						sb.append(li.text()).append(">");
					}
					String category = sb.substring(0, sb.lastIndexOf(">"));
					System.out.println(category);
					returnObj.put("category", category);
				}
				
				//获取名称
				Elements nameClass = doc.getElementsByClass("product-title font-alpha");
				if(null != nameClass && nameClass.size() == 1) {
					System.out.println(nameClass.get(0).text());
					returnObj.put("name", nameClass.get(0).text());
				}
				
				//获取价格
				Elements priceClass = doc.getElementsByClass("product-price");
				if(null != priceClass && priceClass.size() == 1) {
					System.out.println(priceClass.get(0).text());
					returnObj.put("price", priceClass.get(0).text());
				}
				
				//获取图片
				Elements imgClass = doc.getElementsByClass("list-menu product-thumbnails tac");
				if(null != imgClass && imgClass.size() == 1) {
					Elements aTags = imgClass.get(0).getElementsByTag("img");
					StringBuffer sb = new StringBuffer();
					for(Element a : aTags) {
						String trim = StringUtils.trim(a.attr("src"));
						String replace = trim.replace("/70", "/300");
						System.out.println(replace);
						//String strip = StringUtils.strip(a.attr("href"));
						sb.append(replace).append(",");
					}
					String imgLink = sb.substring(0, sb.lastIndexOf(","));
					returnObj.put("image", imgLink);
				}
				
				//获取描述
				Elements descPClass = doc.getElementsByClass("tabs__panel panels cf js-prodInfo-description");
				if(null != descPClass && descPClass.size() == 1) {
					Elements elementsByClass = descPClass.get(0).getElementsByClass("tabs__content panel-content");
					System.out.println(elementsByClass.get(0).text());
					returnObj.put("description", elementsByClass.get(0).text());
				}
				
				//获取产品详情
				Element detailId = doc.getElementById("technicaldetails");
				if(null != detailId) {
					Elements elementsByTag = detailId.getElementsByTag("table");
					System.out.println(elementsByTag.text());
					returnObj.put("detail", elementsByTag.text());
				}
				
				return returnObj.toJSONString();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
